package com.bookcity.controller;

import com.alibaba.fastjson.JSONObject;
import com.bookcity.pojo.BriefBook;
import com.bookcity.pojo.DetailBook;
import com.bookcity.pojo.Recpurchase;
import com.bookcity.service.BookService;
import com.bookcity.service.RecpurchaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * RestBookController
 *
 * @Author : lostprobe
 * @Date : 2022/6/30 17:57
 * @Brief : RestBookController
 */

@Controller
@RequestMapping("/book")
public class RestBookController {
    @Autowired
    private BookService bookService;

    @Autowired
    private RecpurchaseServiceImpl recpurchaseService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    private String listBook(@RequestParam("page") int pn,
                               @RequestParam("per_page") int rn,
                               @RequestParam("type") int type,
                               @RequestParam("zone") int zone,
                               @RequestParam("other") int other) {
        List<BriefBook> books = this.bookService.listBook(pn * rn, rn, type, zone, other);
        JSONObject res = new JSONObject();
        res.put("status_code", 200);
        res.put("message", "success");
        res.put("count", books.size());
//        res.put("total", 1);
//        res.put("total_page", 1);
//        res.put("current_page", 1);
        res.put("data", books);
        return res.toJSONString();
    }

//    控制跳转到 detail 页面
    @RequestMapping(value = "{bid}", method = RequestMethod.GET)
    private String goToDetail(@PathVariable("bid") long bid) {
        return "detail";
    }

//    detail 页面加载时从此获取数据
    @RequestMapping(value = "detail/{bid}", method = RequestMethod.GET)
    @ResponseBody
    private String getBookById(@PathVariable("bid") String _bid) {
        JSONObject res = new JSONObject();
        int bid = Integer.parseInt(_bid);
        DetailBook book = this.bookService.getBookById(bid);
        res.put("book", book);
        JSONObject comment = new JSONObject();
        List data = this.bookService.getComment(bid);
        comment.put("count", data.size());
        comment.put("data", data);
        res.put("comment", comment);

        return res.toJSONString();
    }

    @ResponseBody
    @RequestMapping("/search")
    public String SearchOnBook(@RequestParam("kw") String bookname){
        List<BriefBook> books = bookService.searchBooksByName(bookname);
        JSONObject res = new JSONObject();
        res.put("count", books.size());
        res.put("data", books);
        return res.toJSONString();
    }

    @ResponseBody
    @RequestMapping("/buy")
    public String BuyBook(@RequestParam("uid") int uid,
                          @RequestParam("bid") int bid,
                          @RequestParam("time") long time,  // 秒
                          @RequestParam("num") int num,
                          @RequestParam("price") BigDecimal price) {
        DetailBook book = bookService.getBookById(bid);
        JSONObject res = new JSONObject();
        if(book.getStock() > num) {
//        减去对应的库存
            bookService.updateBookStockById(bid, num);
            Recpurchase recpurchase = new Recpurchase();
            recpurchase.setUid(uid);
            recpurchase.setInencoding(bid);
            recpurchase.setPurchasetime(LocalDateTime.ofEpochSecond(time, 0, ZoneOffset.ofHours(8)));
            recpurchase.setHandle(price);
            recpurchase.setPaid(price);
            recpurchase.setNumber(num);
            res.put("state", recpurchaseService.addRecpurchase(recpurchase));
        } else {
            res.put("state", 0);
        }
        return res.toJSONString();
    }
}
